package com.qifan.energy.util;

import android.content.Context;
import android.widget.TextView;

import com.qifan.energy.BuildConfig;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 基本检查工具类
 *
 * @author xuan
 * @date 2018/3/26.
 * 静态方法
 */

public final class CheckUtil {

    private CheckUtil() {}

    public static int size(Collection list) {
        if (isEmpty(list)) {
            return 0;
        }
        return list.size();
    }

    /**
     * 判断字符
     *
     * @param text 字符
     * @return 值状态
     */
    public static boolean hasContent(CharSequence text) {
        return text != null && text.toString().trim().length() > 0;
    }

    /**
     * 判断字符
     *
     * @param text 字符
     * @return 值状态
     */
    public static boolean isEmpty(CharSequence text) {
        return text == null || text.length() == 0;
    }

    /**
     * 判断字符
     *
     * @return 值状态
     */
    public static boolean isEmpty(ConcurrentHashMap concurrentHashMap) {
        return concurrentHashMap == null || concurrentHashMap.size() == 0;
    }

    /*
     * 判断是否长度小于<
     * */
    public static boolean mixThen(String text, int size) {
        return hasContent(text) && text.length() < size;
    }

    /*
     * 判断是否长度小于<
     * */
    public static boolean mixThenByte(String text, int size) {
        return hasContent(text) && text.getBytes(StandardCharsets.UTF_8).length < size;
    }

    public static boolean mixThen(Collection list, int size) {
        return hasContent(list) && list.size() < size;
    }

    /*
     * 判断是否长度大于>
     * */
    public static boolean maxThen(String text, int size) {
        return hasContent(text) && text.length() > size;
    }

    /*
     * 判断是否长度大于>
     * */
    public static boolean maxThenByte(String text, int size) {
        return hasContent(text) && text.getBytes(StandardCharsets.UTF_8).length > size;
    }

    public static boolean maxThen(Collection list, int size) {
        return hasContent(list) && list.size() > size;
    }

    /**
     * 判断集合
     *
     * @param list 集合
     * @return 集合状态
     */
    public static boolean hasContent(Collection list) {
        return list != null && list.size() > 0;
    }

    /**
     * 判断集合
     *
     * @param list 集合
     * @return 集合状态
     */
    public static boolean isEmpty(Collection list) {
        return list == null || list.size() == 0;
    }

    /**
     * 判断是否有输入
     */
    public static boolean hasContent(TextView view) {
        return view != null && hasContent(view.getText());
    }

    /**
     * 判断是否有输入
     */
    public static boolean isEmpty(TextView view) {
        return view == null || isEmpty(view.getText());
    }

    /**
     * 是否是合法的网址
     *
     * @param url url
     * @return url合法否
     */
    public static boolean isLegalWebSite(String url) {
        if (!hasContent(url)) {
            return false;
        }
        String regExp = "\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(url);
        return m.matches();
    }

    /**
     * 是否是路径格式
     */
    public static boolean isPath(String path) {
        if (isEmpty(path)) {
            return false;
        }
        String regExp = "[/\\\\]+(\\w+\\.)+[a-zA-Z]+$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(path);
        return m.matches();
    }

    /**
     * 比较文字信息
     *
     * @param v1 文字控件
     * @param v2 文字控件
     * @return 类似否
     */
    public static boolean equals(TextView v1, TextView v2) {
        return v1 != null && v2 != null && v1.getText().toString().equals(v2.getText().toString());
    }

    /**
     * 检测IP和掩码DNS等格式文本
     */
    public static boolean ipLike(String text) {
        String regExp = "^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$";
        Pattern compile = Pattern.compile(regExp);
        Matcher matcher = compile.matcher(text);
        return matcher.matches();
    }

    /**
     * 判断微信是否安装
     * @param context
     * @return true 已安装   false 未安装
     */
    public  static boolean isWxAppInstalled(Context context) {
        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, null);
        wxApi.registerApp(BuildConfig.WX_APPKEY);
        boolean bIsWXAppInstalled = false;
        bIsWXAppInstalled = wxApi.isWXAppInstalled();
        return bIsWXAppInstalled;
    }
}
